//
//  WmcGeneralTemplateView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/7.
//

import UIKit

/// 工程水印模板
class WmcProjectTemplateView: WmcBaseWatermarkTemplateView {
    /// 品牌图
    var brandView: WmcBaseBrandView?

    /// 品牌图背景视图
    lazy var brandBackgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()

    lazy var titleView: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var spacingView: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var cornersView: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 4
        view.layer.masksToBounds = true
        return view
    }()

    override func config() {
        super.config()
        defaultWidth = 225.wmc_autoHor
        editPreviewBrandContentShowBrand = false // 编辑页不展示外部预览

        addSubview(cornersView)
        
        cornersView.addSubview(titleView)
        titleView.addSubview(brandBackgroundView)
        titleView.addSubview(titleLabel)

        cornersView.addSubview(spacingView)
        
        cornersView.addSubview(stackView)
        stackView.layer.cornerRadius = 0
        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 0
    }

    override func configWatermarkModel(_ watermarkModel: WmcWatermarkDetailModel, styleMode: WmcBaseWatermarkTemplateView.StyleMode) {
        super.configWatermarkModel(watermarkModel, styleMode: styleMode)
        // 设置透明度
        let defaultAlpha = calculateAlpha(watermarkModel.alpha, min: 0.5, max: 1)
        let settingAlpha = calculateAlpha(watermarkModel.alpha, min: 0, max: 1)

        spacingView.backgroundColor = .black.yp.alpha(defaultAlpha)
        
        var drawViews: [UIView] = []
        // 获取标题最大字数
        let maxTitleCount = watermarkModel.fieldTitleMaxCount
        watermarkModel.fields.forEach { option in
            guard option.isOpen else { return }
            switch option.fieldType {
            case .title: // 标题
                titleLabel.text = option.content
                titleLabel.textColor = option.uiTextColor
                if option.bgColor.isEmpty {
                    titleLabel.backgroundColor = .wmc_mainTheme.yp.alpha(defaultAlpha)
                    brandBackgroundView.backgroundColor = UIColor.white.yp.alpha(defaultAlpha)
                } else {
                    titleLabel.backgroundColor = option.uiBgColor.yp.alpha(settingAlpha)
                    brandBackgroundView.backgroundColor = UIColor.white.yp.alpha(settingAlpha)
                }

            case .time: // 时间
                let item = createDateItem(option: option,
                                          styleMode: styleMode,
                                          staticDate: watermarkModel.staticData.staticDate,
                                          titleStyle: .noTitle,
                                          maxTitleCount: maxTitleCount,
                                          dateFormat: .yyyy_MM_dd_week_HH_mm)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }
                drawViews.append(item)

            case .addr: // 地点
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .location)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }
                drawViews.append(item)

            case .lat: // 经纬
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .longitudLatitude)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }

                drawViews.append(item)

            case .weather: // 天气
                let item = createWeatherItem(option: option,
                                             styleMode: styleMode,
                                             staticWeater: watermarkModel.staticData.staticWeater,
                                             maxTitleCount: maxTitleCount,
                                             weatherFormat: .weatherTemperature)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }
                drawViews.append(item)

            case .altitude: // 海拔
                let item = createAltitudeItem(option: option,
                                              styleMode: styleMode,
                                              staticLocation: watermarkModel.staticData.staticLocation,
                                              maxTitleCount: maxTitleCount,
                                              altitudeFormat: .meters)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }
                drawViews.append(item)

            case .azimuth: // 方位角
                let item = createAzimuthItem(option: option,
                                             styleMode: styleMode,
                                             staticAzimuth: watermarkModel.staticData.staticAzimuth,
                                             maxTitleCount: maxTitleCount,
                                             azimuthFormat: .default)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }
                drawViews.append(item)

            case .decibe: // 分贝
                let item = createDecibeItem(option: option,
                                            styleMode: styleMode,
                                            maxTitleCount: maxTitleCount,
                                            decibeFormat: .describeDB)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }
                drawViews.append(item)

            case .speed: // 速度
                let item = createSpeedItem(option: option,
                                           styleMode: styleMode,
                                           staticLocation: watermarkModel.staticData.staticLocation,
                                           maxTitleCount: maxTitleCount,
                                           speedFormat: .km_h)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }
                drawViews.append(item)

            case .remark,
                 .record,
                 .text,
                 .custom: // 备注、记工、输入
                let item = createDefaultTextItem(option: option,
                                                 maxTitleCount: maxTitleCount)
                if option.bgColor.isEmpty {
                    item.backgroundColor = .black.yp.alpha(defaultAlpha)
                } else {
                    item.backgroundColor = option.uiBgColor(.black).yp.alpha(settingAlpha)
                }
                drawViews.append(item)

            default: break
            }
        }

        // 添加进堆栈视图
        stackView.yp.arrangedSubviews = drawViews
        
        // 布局
        makeConstraints()
        
        // 配置品牌图
        configBrand(watermarkModel)
    }

    func configBrand(_ watermarkModel: WmcWatermarkDetailModel) {
        // 品牌图处理
        if let option = watermarkModel.optionBrand() {
            let brand = option.brand
            // 移除之前的水印
            brandView?.removeFromSuperview()
            brandView = nil
            removeBrand()
            if option.isOpen {
                if brand.locationType == .followWatermark { // 跟随水印
                    brandView = .CreateBrandTemplateView(option) // 创建品牌图
                    guard let brandView = brandView else { return }
                    brandView.maxSize = CGSize(width: 62, height: 42)

                    brandBackgroundView.isHidden = false
                    brandBackgroundView.addSubview(brandView)
                    brandView.brandFitSizeBlock = { [weak self] size in
                        guard let weakSelf = self,
                              let brandView = weakSelf.brandView else { return }
                        brandView.contentView.layer.cornerRadius = 0 // 不要圆角
                        brandView.snp.makeConstraints { make in
                            make.left.equalTo(4)
                            make.right.equalTo(-4)
                            make.centerY.equalToSuperview()
                            make.height.equalTo(size.height)
                        }
                        let brandWidth = size.width + 8
                        // 更新品牌背景视图宽度
                        weakSelf.brandBackgroundView.snp.updateConstraints { make in
                            make.width.equalTo(brandWidth)
                        }
                        // 更新标题Label左边距
                        weakSelf.titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(brandWidth)
                        }
                        weakSelf.titleLabel.font = 18.yp.uiFont(weight: .medium)
                    }
                    brand.size = 20
                    brandView.configOptionBrand(option)
                    // 更新标题视图高度
                    titleView.snp.updateConstraints { make in
                        make.height.equalTo(50)
                    }
                } else { // 不跟随水印
                    configOptionBrand(option)
                    brandBackgroundView.isHidden = true
                    // 更新标题视图高度
                    titleView.snp.updateConstraints { make in
                        make.height.equalTo(30)
                    }
                    // 更新标题Label左边距
                    titleLabel.snp.updateConstraints { make in
                        make.left.equalTo(0)
                    }
                    titleLabel.font = 14.wmc_font
                }
            } else {
                brandBackgroundView.isHidden = true
                // 更新标题视图高度
                titleView.snp.updateConstraints { make in
                    make.height.equalTo(30)
                }
                // 更新标题Label左边距
                titleLabel.snp.updateConstraints { make in
                    make.left.equalTo(0)
                }
                titleLabel.font = 14.wmc_font
            }
        }
    }

    override func makeConstraints() {
        super.makeConstraints()
        var isUseTitle = false
        watermarkModel?.fields.forEach { option in
            if option.fieldType == .title,
               option.isOpen
            {
                isUseTitle = true
            }
        }
        if isUseTitle {
            titleView.isHidden = false
            
        }else{
            titleView.isHidden = true
        }
        
        cornersView.snp.remakeConstraints { make in
            make.top.left.equalTo(8)
            make.right.equalTo(-8)
            if isShowCertificationWatermark {
                make.bottom.equalTo(certificationView.snp.top).offset(-4)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }
        
        titleView.snp.remakeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(30)
        }

        brandBackgroundView.snp.remakeConstraints { make in
            make.top.left.bottom.equalToSuperview()
            make.width.equalTo(0.01)
        }

        titleLabel.snp.remakeConstraints { make in
            make.top.bottom.right.equalToSuperview()
            make.left.equalTo(0.001)
        }

        spacingView.snp.remakeConstraints { make in
            if isUseTitle {
                make.top.equalTo(titleView.snp.bottom)
            } else {
                make.top.equalToSuperview()
            }
            make.left.right.equalToSuperview()
            make.height.equalTo(8)
        }
        
        stackView.snp.remakeConstraints { make in
            make.top.equalTo(spacingView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
}
